import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/systems/Welcome.vue'
import Departments from '../components/systems/Departments.vue'
import Roles from '../components/systems/Roles.vue'
import Menus from '../components/systems/Menus.vue'
import Attachments from '../components/systems/Attachments.vue'
import Users from '../components/systems/Users.vue'
import Logs from '../components/data/Logs.vue'
import LoginLog from '../components/data/LoginLog.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },

  // 日志模块
  {
    path: '/login',
    name: 'Login',
    component: Login
  },


  // 系统模块
  {
    path: '/home',
    component: Home,
    meta:{title: '首页'},
    children: [
      {
        path: '/welcome',
        component: Welcome
      }, {
        path: '/departments',
        component: Departments,
      }, {
        path: '/roles',
        component: Roles,
        meta:{title: '角色管理'},
      }, {
        path: '/menus',
        component: Menus,
      }, {
        path: '/attachments',
        component: Attachments
      }, {
        path: '/users',
        component: Users,
        meta:{title: '用户管理'},
      }, {
        path: '/logs',
        component: Logs
      }, {
        path: '/loginLog',
        component: LoginLog
      }
    ]
  },

  // 业务管理模块
  {
    path:'/bus',
    component:Home,
    children:[
      {path:'/inStocks',component:()=> import ("@/components/bussiness/management/inStocks")},
      {path:'/products',component:()=> import ("@/components/bussiness/management/products")},
      {path:'/outStocks',component:()=> import ("@/components/bussiness/management/outStocks")},
      {path:'/out-stocks',component:()=> import ("@/components/bussiness/management/out-stocks")},

      {path:'/productCategorys',component:()=> import ("@/components/bussiness/management/productCategorys")},

      {path:'/stocks',component:()=> import ("@/components/bussiness/management/stocks")},
      {path:'/add-stocks',component:()=> import ("@/components/bussiness/management/add-stocks")},

      {path:'/consumers',component:()=> import ("@/components/bussiness/flow/consumers")},
      {path:'/suppliers',component:()=> import ("@/components/bussiness/flow/suppliers")},

    ]
  },

  // 健康报备模块
  {
    path:'/hea',
    component:Home,
    children:[
      {path:'/health',component:()=> import ("@/components/health/health")},
      {path:'/map',component:()=> import ("@/components/health/map")},
      {path:'/rumors',component:()=> import ("@/components/health/rumors")},

    ]
  },
]



const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
})

import store from '../store'//引入store

//路由导航守卫
// router.beforeEach((to, from, next) => {
//
//   const token = window.localStorage.getItem('JWT_TOKEN');
//   if (to.path == '/login') {
//     if(!token){
//       return next();
//     }else{
//       return next({path: '/home'})
//     }
//   }
//
//   if(to.path=='/401'){
//     return next();
//   }
//
//   if (!token) {
//     return next('/login');
//   }else {
//     //判断是否有访问该路径的权限
//     const urls =store.state.userInfo.url;
//     //如果是管理员
//     if(store.state.userInfo.isAdmin){
//       return next();
//     }else{
//       if(urls.indexOf(to.path) > -1){
//         //则包含该元素
//         window.sessionStorage.setItem("activePath", to.path);
//         return next();
//       }else{
//         return next("/401");
//       }
//     }
//   }
// })

export default router
